package com.example.controller;

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Api(tags = { "web服务：minio上传,下载,删除接口" })
@RestController
@RequestMapping("view/minio")
public class FileUploadController {

    @Autowired
    private MinioClient minioClient;

    private static final String BUCKET = "img";


    /**
     * @Description: 下载文件
     * @Author: zhengyue
     * @Date: 2020/10/29 9:42
     * @Param: [fileName, response]
     * @Return: void
     */
    @ApiOperation(value = "下载文件")
    @GetMapping(value = "/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        InputStream in = null;
        final ObjectStat stat = minioClient.statObject(BUCKET, fileName);
        response.setContentType(stat.contentType());
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        in = minioClient.getObject(BUCKET, fileName);
        IOUtils.copy(in, response.getOutputStream());
        in.close();
    }

   /**
    * @Description: 上传文件
    * @Author: zhengyue
    * @Date: 2020/10/29 9:47
    * @Param: [file]
    * @Return: ResponseMsg<String>
    */
    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RuntimeException("上传文件不能为空");
        } else {
            if(!minioClient.bucketExists(BUCKET)) {
                minioClient.makeBucket(BUCKET);
            }
            // 得到文件流
            final InputStream is = file.getInputStream();
            // 文件名
            final String fileName = file.getOriginalFilename();
            // 把文件放到minio的boots桶里面
            minioClient.putObject(BUCKET, fileName, is,
                    new PutObjectOptions(is.available(), -1));
            // 关闭输入流
            is.close();
            String url = minioClient.getObjectUrl(BUCKET, fileName);
            return "上传成功, " + url;
        }
    }

    /**
     * @Description: 删除文件
     * @Author: zhengyue
     * @Date: 2020/10/29 9:48
     * @Param: [fileName]
     * @Return: ResponseMsg<String>
     */
    @ApiOperation(value = "删除文件")
    @GetMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestParam("fileName") String fileName) {
        try {
            minioClient.removeObject(BUCKET, fileName);
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }

}